//
//  SNArtelViewController.m
//  ZhuoxinAgriculture
//
//  Created by stone on 16/12/16.
//  Copyright © 2016年 stone. All rights reserved.
//

#import "SNMyArtelViewController.h"
#import "UIImageView+TapGesture.h"
#import "SNAssociatorViewController.h"
#import "SNArtelViewController.h"
#import "Masonry.h"
#import "SNArtelCell.h"
@interface SNArtelViewController ()<UITableViewDataSource,UITableViewDelegate>
/** tableView */
@property (nonatomic,weak) UITableView * tableView;
/** View */
@property (weak, nonatomic) UIImageView * bgImageView;
/** View */
@property (weak, nonatomic)  UIImageView * iconImageView ;
/** View */
@property (weak, nonatomic) UILabel * titleLabel ;
/** numberLabel */
@property (weak, nonatomic) UILabel * numberLabel;
@end

@implementation SNArtelViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HexRGB(0xf5f5f5);
    self.navigationItem.title = @"农户列表";
    [self addViews];
    [self setupUI];
    
    // Do any additional setup after loading the view, typically from a nib.
    dispatch_async(dispatch_get_main_queue(), ^{
        [self addTableView];
        
        
        
    });
    [self.navigationController.navigationBar setBackgroundImage:[SNUtils imageWithColor:[UIColor clearColor]] forBarMetrics:UIBarMetricsDefault];
    [self.navigationController.navigationBar setShadowImage:[SNUtils imageWithColor:[UIColor clearColor]]];
    
    
    
}
- (void)addTableView
{
    CGRect frame = CGRectMake(0, CGRectGetMaxY(self.bgImageView.frame), [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - CGRectGetMaxY(self.bgImageView.frame));
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStyleGrouped];
    self.tableView = tableView;
    [self setupInit:tableView];
    
    [self.view addSubview:tableView];
    
    [self addObserver];
    [self addRequest];
}
- (void)addViews
{
    UIImageView * bgImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        //        imageView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
        //        UIImage * image = [UIImage imageNamed:@"01-sili"];
        //        imageView.image = image;
        
        // [self addSubview:imageView];
        UIImage * bgImage = [UIImage imageNamed:@"bgimage"];
        //        imageView.backgroundColor = [UIColor colorWithPatternImage:bgImage];
        imageView.image = bgImage;
        [self.view addSubview:imageView];
        
        imageView;
    });
    self.bgImageView = bgImageView;
    
    
    UIImageView * iconImageView = ({
        UIImageView * imageView = [[UIImageView alloc] init];
        //        imageView.backgroundColor = [UIColor colorWithWhite:1.0 alpha:1.0];
        //        UIImage * image = [UIImage imageNamed:@"01-sili"];
        //        imageView.image = image;
        
        // [self addSubview:imageView];
        UIImage * bgImage = [UIImage imageNamed:self.iconImageName];
        //        imageView.backgroundColor = [UIColor colorWithPatternImage:bgImage];
        imageView.image = bgImage;
        [self.view addSubview:imageView];
        
        @weakify_self
        imageView.sn_tapBlock = ^(){
            @strongify_self
            SNMyArtelViewController *vc = [[SNMyArtelViewController alloc]init];
            vc.userId = @"";
            vc.companyNo = self.COMPANYNO;
            [self.navigationController pushViewController:vc animated:YES];
        };
        
        imageView;
    });
    self.iconImageView = iconImageView;
    
    UILabel * titleLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        //        label.backgroundColor = [UIColor whiteColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 1;
        label.textColor = [UIColor blackColor];
        label.textAlignment = NSTextAlignmentRight;
        label.text = ([self.SHORTNAME isEqualToString:@""] || self.SHORTNAME == nil)?@"未知":self.SHORTNAME;
        label.font = [UIFont systemFontOfSize:15];
        
        [self.view addSubview:label];
        
        label;
    });
    self.titleLabel = titleLabel;
    
    
    
    UILabel * numberLabel = ({
        UILabel * label = [[UILabel alloc] init];
        /** GPU 优化 */
        //        label.opaque = YES;
        label.backgroundColor = [UIColor redColor];
        //        label.layer.masksToBounds = YES;
        //------------------------------
        label.numberOfLines = 0;
        label.textColor = [UIColor whiteColor];
        label.textAlignment = NSTextAlignmentCenter;
#warning 坑爹的服务器...
        label.text = [NSString stringWithFormat:@"%@人",self.MEMBERQTY];
        label.font = [UIFont systemFontOfSize:15];
        
        [self.view addSubview:label];
        
        
        label;
    });
    self.numberLabel = numberLabel;
}
- (void)setupUI
{
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.centerX.mas_equalTo(self.view.mas_centerX).offset(0);
        make.top.mas_equalTo(self.view.mas_top).offset(75);
        
        make.width.mas_equalTo(90);
        make.height.mas_equalTo(90);
        
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        
        make.left.mas_equalTo(self.view.mas_left).offset(15);
        
        make.right.mas_equalTo(self.iconImageView.mas_left).offset(57);
        make.top.mas_equalTo(self.iconImageView.mas_bottom).offset(14);
        
    }];
    
    CGSize size = [[[NSAttributedString alloc] initWithString:self.numberLabel.text attributes:@{NSFontAttributeName : self.numberLabel.font}] boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading context:nil].size;
    [self.numberLabel mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.titleLabel.mas_right).offset(8);
        make.centerY.mas_equalTo(self.titleLabel.mas_centerY).offset(0);
        
        make.width.mas_equalTo(size.width+10+10);
        make.height.mas_equalTo(size.height+2.5+2.5);
    }];
    
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker * make) {
        make.left.mas_equalTo(self.view.mas_left).offset(0);
        make.right.mas_equalTo(self.view.mas_right).offset(0);
        make.top.mas_equalTo(self.view.mas_top).offset(0);
        //13
        make.bottom.mas_equalTo(self.titleLabel.mas_bottom).offset(13);
        
    }];
}
#pragma mark - <addRequest>
- (void)addRequest {
    // addRequest
}
#pragma mark - <addObserver>
- (void)addObserver {
    // addObserver
}
- (void)dealloc {
    NSLog(@"%@ %s %@",[self class], __func__ , @"im dying");
}

- (void)setupInit:(UITableView *)tableView {
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = HexRGB(0xf5f5f5);
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView * headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 12)];
    tableView.tableHeaderView = headerView;
    [self setHeights:tableView];
    [self registerNib:tableView];
    [self registerClass:tableView];
}
#pragma mark - <Register>
/** registerNib */
- (void)registerNib:(UITableView *)tableView {
    //        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SNArtelCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([SNArtelCell class])];
}
/** registerClass */
- (void)registerClass:(UITableView *)tableView {
    //    [tableView registerClass:[SNTableViewCell class] forCellReuseIdentifier:NSStringFromClass([SNTableViewCell class])];
    [tableView registerClass:[SNArtelCell class] forCellReuseIdentifier:NSStringFromClass([SNArtelCell class])];
    //    [tableView registerClass:[UITableViewHeaderFooterView class] forHeaderFooterViewReuseIdentifier:NSStringFromClass([UITableViewHeaderFooterView class])];
}
#pragma mark - <SetHeights>
/** setHeights */
- (void)setHeights:(UITableView *)tableView {
    
    /** header */
    tableView.sectionHeaderHeight = 12;
    // tableView.estimatedSectionHeaderHeight = 100;
    // tableView.sectionHeaderHeight = UITableViewAutomaticDimension;
    /** cell */
    tableView.estimatedRowHeight = 100;
    tableView.rowHeight = UITableViewAutomaticDimension;
    /** footer */
    tableView.sectionFooterHeight = 0.001;
    // tableView.estimatedSectionFooterHeight = 100;
    // tableView.sectionFooterHeight = UITableViewAutomaticDimension;
}

#pragma mark - <UITableViewDataSource>
/** 组数 */
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.custinfolist.count;
}
/** 行数 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
//MARK: -
//MARK: - cell
/** cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SNArtelCell * cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SNArtelCell class])];
    
    [cell configWithModel:self.custinfolist[indexPath.section] shortname:self.SHORTNAME];
    
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}
#pragma mark - <UITableViewDelegate>

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return 12;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return 0.001;
}
/** 选中一行 */
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%s", __func__);
    
    SNAssociatorViewController * vc = [SNAssociatorViewController new];
    vc.USERID = [self.custinfolist[indexPath.section] userId];
    [self.navigationController pushViewController:vc animated:YES];
}
/** 取消 选中 */
- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(3_0) {
    NSLog(@"%s", __func__);
}
/** 可编辑 */
//- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return YES;
//}
/** 删除样式 */
//- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    return UITableViewCellEditingStyleDelete;
//}
/** 删除当前行 */
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
//
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//
//        [self.models removeObjectAtIndex:indexPath.row];
//
//        [tableView deleteRowsAtIndexPaths:@[ indexPath ] withRowAnimation:UITableViewRowAnimationLeft];
//}
@end
